package org.zhou.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhou.constant.MessageConstant;
import org.zhou.pojo.*;
import org.zhou.service.BooksService;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/books")
@Slf4j
public class BooksController {

    @Autowired
    private BooksService booksService;

    @GetMapping("/classify")
    public Result<List<Type>> getClassify(){

        log.info("查询分类");
        List<Type> listType = booksService.getClassify();
        return Result.success(listType);
    }

    @PostMapping("/getByNameClassify")
    public Result<List<Type>> getByNameClassify(@RequestBody Type type){
        log.info("根据id查询分类：{}",type);
        List<Type> typeList = booksService.getByNameClassify(type.getName());
        log.info("分类{}",typeList);
//        if(typeList.size() > 0){
            return Result.success(typeList);
//        }else {
//            return Result.error("未查询到");
//        }

    }

    @PostMapping("/addClassify")
    public Result addClassify(@RequestBody Type type){
        log.info("添加分类：{}",type);
        booksService.addClassify(type.getName());
        return Result.success();
    }

    @PutMapping("/updateClassify")
    public Result updateClassify(@RequestBody Type type){
        log.info("修改分类，{}",type);
        booksService.updateClassify(type);
        return Result.success();

    }

    @DeleteMapping("/deleteClassify")
    public Result deleteClassify(Integer id){
        log.info("删除分类：{}",id);
        booksService.deleteClassify(id);
        return Result.success();

    }

    @GetMapping
    public Result<PageResult> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String bookname,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) String bookId
    ) {
        log.info("分页查询图书，{},{},{},{},{},{}",pageNum,pageSize,bookname,categoryId,author,bookId);
        PageResult pb =  booksService.list(pageNum,pageSize,bookname,categoryId,author,bookId);
        return Result.success(pb);
    }

    @PostMapping("/addBook")
    public Result addBook(@RequestBody Book book){
        log.info("添加图书，{}",book);
        if(book.getBookname().length()>20 || book.getBookname().length()==0){
            return Result.error(MessageConstant.ERROR);
        }
        if(book.getIdNumber().length()>10){
            return Result.error(MessageConstant.ERROR);
        }
        List bookList = booksService.list(1,1,null,null,null,book.getIdNumber())
                                            .getItems();
        if(bookList.size() > 0){
            return Result.error("该图书编号已存在");
        }
        booksService.addBook(book);
        return Result.success();
    }

    @PutMapping("/updateBook")
    public Result updateBook(@RequestBody Book book){
        log.info("修改图书，{}",book);
        if(book.getBookname().length()>20 || book.getBookname().length()==0){
            return Result.error(MessageConstant.ERROR);
        }
        booksService.updateBook(book);
        return Result.success();
    }

    @DeleteMapping("/deleteBook")
    public Result deleteBook(Integer id){
        log.info("删除图书：{}",id);
        booksService.deleteBook(id);
        return Result.success();

    }

    @GetMapping("/borrow")
    public Result<PageResult> borrowList(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String readerId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String bookId
    ) {
        log.info("分页查询借阅表，{},{},{},{},{}",pageNum,pageSize,readerId,state,bookId);
        PageResult pb =  booksService.borrowList(pageNum,pageSize,readerId,state,bookId);
        log.info("查询结果，{}",pb.getItems());
        return Result.success(pb);
    }

    @PostMapping("/checkBorrow")
    public Result<BorrowVO> getBorrow(@RequestBody CheckBorr cb){
        String readerId = cb.getReaderId();
        String bookId = cb.getBookId();
        log.info("核验借阅详情：{}",cb);

        if(readerId.length()==0 || bookId.length()==0){
            return Result.error(MessageConstant.ERROR);
        }
        BorrowVO borrowVO = booksService.getBorrow(cb);
        log.info("查询结果；{}",borrowVO);
        if(borrowVO == null){
            return Result.error(MessageConstant.ERROR);
        }
        return Result.success(borrowVO);

    }

    @PostMapping("/addBorrow")
    public Result addBorrow(@RequestBody CheckBorr cb){
        String readerId = cb.getReaderId();
        String bookId = cb.getBookId();
        log.info("借阅人：{}，图书：{}",readerId,bookId);

        BorrowVO borrow = booksService.getBorrow(cb);
        if(borrow.getReaderStat()==1){
            return Result.error(MessageConstant.READER_LOCKED);
        }
        if(borrow.getBalance().compareTo(BigDecimal.valueOf(0.0))<0){
            return Result.error(MessageConstant.BALANCE_ERROR);
        }

        booksService.addBorrow(readerId,bookId);
        return Result.success();
    }

    @PutMapping("/returnBook")
    public Result returned (@RequestBody CheckBorr cb){
//        BigDecimal balance = cb.getBalance();
        log.info("还书：{}",cb);
        booksService.returned(cb);
        return Result.success();
    }


}
